package com.dkm.test;

import java.util.concurrent.TimeUnit;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 发送同步消息
 * @author 狄凯明
 *
 */
public class SyncProducer {

	public static void main(String[] args) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
		//1.创建消息生产者producer,并指定生产者组名
		DefaultMQProducer producer=new DefaultMQProducer("group1");
		
		//2.指定Nameserver地址
		producer.setNamesrvAddr("127.0.0.1:9876");
		//3.启动producer
		producer.start();
		
		for (int i = 0; i < 2; i++) {
			//4.创建消息对象，指定主题topic，tag和消息体
			/**
			 * 参数一：消息主题Topic
			 * 参数二：消息Tag
			 * 参数三：消息内容
			 */
			Message message=new Message("base","tag1",("hello 狄凯明").getBytes());
			
			//5.发送消息
			SendResult result=producer.send(message);
			//发送状态
			SendStatus status=result.getSendStatus();
			System.out.println("result>"+result);
			
			//线程睡眠1秒
			TimeUnit.SECONDS.sleep(1);
		}
		
		//6.关闭生产者producer
		producer.shutdown();
	}
}
